package com.cbn.ibcp.web.jishiting.operator.controller;

import com.cbn.ibcp.jishiting.gs.cms.service.*;
import com.cbn.ibcp.web.jishiting.operator.dto.CdnDataStatusDTO;
import com.cbn.ibcp.common.core.controller.BaseController;
import com.cbn.ibcp.common.core.domain.PageResult;
import com.cbn.ibcp.common.core.page.TableDataInfo;
import com.cbn.ibcp.jishiting.gs.cms.domain.CpVideo;
import com.cbn.ibcp.jishiting.gs.cms.domain.Node;
import com.cbn.ibcp.jishiting.gs.cms.domain.Org;
import com.cbn.ibcp.jishiting.rms.domain.ResVideoEpisode;
import com.cbn.ibcp.jishiting.rms.service.IResVideoEpisodeService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/cdn/cdnTool")
@RequiredArgsConstructor
public class CdnToolController extends BaseController {

    private final IOrgService orgService;

    private final INodeService nodeService;

    private final ICpVideoService cpVideoService;

    private final IResVideoEpisodeService resVideoEpisodeService;

    @GetMapping("/getDataStatusList")
    public PageResult<CpVideo> getDataStatusListByParam(CdnDataStatusDTO cdnDataStatusDTO) {
        if (Objects.isNull(cdnDataStatusDTO)) {
            return PageResult.fail("cdnDataStatusDTO 不存在");
        }
        String orgCode = cdnDataStatusDTO.getOrgCode();
        Org org = orgService.selectByOrgCode(orgCode);
        if (org == null) {
            return PageResult.fail("orgCode 不存在");
        }
        Node node = nodeService.selectByOrgId(org.getId());
        if (node == null) {
            return PageResult.fail("node 不存在");
        }
        Long albumId = cdnDataStatusDTO.getAlbumId();
        Long videoId = cdnDataStatusDTO.getVideoId();
        if (Objects.isNull(albumId) && Objects.isNull(videoId)) {
            return PageResult.fail("albumId 或者 videoId  必须存在");
        }
        //查询所有剧集
        List<ResVideoEpisode> resVideoEpisodes = resVideoEpisodeService.selectListByIdAndAlbumId(videoId, albumId);
        if (CollectionUtils.isEmpty(resVideoEpisodes)) {
            return PageResult.fail("rms 层没有数据");
        }
        startPage();
        List<CpVideo> cpVideoList = cpVideoService.getListByParam(albumId, videoId);
        TableDataInfo dataTable = getDataTable(cpVideoList);
        return PageResult.ok(dataTable.getTotal(), cpVideoList, dataTable.getMsg());
    }
}
